package org.lemon.transport.context.service.queue.stats;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class StatsFactory {

    private static final String TOTAL_MESSAGES = "totalMESSAGES";
    private static final String SUCCESSFUL_MESSAGES = "successfulMESSAGES";
    private static final String FAILED_MESSAGES = "failedMESSAGES";

    private static final String STATS_NAME_TAG = "statsName";

    private static final Counter STUB_COUNTER = new StubCounter();

    @PostConstruct
    public void init() {

    }

    public StatsCounter createStatsCounter(String key, String statsName) {
        return new StatsCounter(
                statsName,
                new AtomicInteger(0),
                STUB_COUNTER
        );
    }

    public MessagesStats createMessagesStats(String key) {
        StatsCounter totalCounter = createStatsCounter(key, TOTAL_MESSAGES);
        StatsCounter successfulCounter = createStatsCounter(key, SUCCESSFUL_MESSAGES);
        StatsCounter failedCounter = createStatsCounter(key, FAILED_MESSAGES);
        return new MessagesStats(totalCounter, successfulCounter, failedCounter);
    }

    private static class StubCounter implements Counter {
        @Override
        public void increment(double amount) {
        }

        @Override
        public double count() {
            return 0;
        }

        @Override
        public Id getId() {
            return null;
        }
    }

}
